/* Copyright Paul Reiche, Fred Ford. 1992-2002, Christopher Night 2013

This file is part of the HTML5 port of Ur-Quan Masters (UQM-HTML5)

UQM-HTML5 is free software: you can redistribute it and/or modify it under the terms of the GNU
General Public License as published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.

UQM-HTML5 is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General
Public License for more details.

You should have received a copy of the GNU General Public License along with this program.  If not,
see <http://www.gnu.org/licenses/>. */


var races = {}

// Base object for other races
// Most of these functions are callbacks that are apply'd onto the ship object
races.base = {
	preprocess_func: function () {},
	postprocess_func: function () {},


	// Races that do something screwy here should overwrite this
	regenerate_energy: function () {
		if (this.energy_counter) {
			--this.energy_counter
		} else if (this.energy_level < this.info.max_energy) {
			this.energy_level = Math.min(
				this.energy_level + this.info.energy_regeneration,
				this.info.max_energy
			)
			this.energy_counter = this.info.max_energy // TODO: this is wrong
		}
	},
	turn_func: function () {
		if (this.turn_wait) {
			--this.turn_wait
		} else {
			var dface = (this.input_state.RIGHT ? 1 : 0) - (this.input_state.LEFT ? 1 : 0)
			this.facing = rmod(this.facing + dface, 16)
			this.next.frame = this.info.frames[this.facing]
			if (dface) this.turn_wait = this.info.turn_wait
		}
	},
	thrust_func: function () {
		// Thrusting
		if (this.thrust_wait) {
			--this.thrust_wait
		} else if (this.input_state.THRUST) {
			this.inertial_thrust()
			this.thrust_wait = this.info.thrust_wait
			if (!this.status.CLOAKED) this.spawn_ion_trail()
		}
	},
	weapon_func: function () {
		// Fire weapon
		if (this.weapon_counter) {
			--this.weapon_counter
		} else if (this.input_state.WEAPON) {
			if (this.energy_level < this.info.weapon_energy_cost) {
				this.status.LOW_ON_ENERGY = true  // TODO: should be reset every frame
				return
			}
			var nweapons = this.race.init_weapon_func.apply(this)
			if (!nweapons || !nweapons.length) return
			this.energy_level -= this.info.weapon_energy_cost
			this.weapon_counter = this.info.weapon_wait
			for (var j = 0 ; j < nweapons.length ; ++j) {
				elements.push(nweapons[j])
				nweapons[j].parent = this
			}
		}
	},
	// This base method is for the races whose specials just require energy like a weapon.
	// Feel free to override for exotic special abilities (Pkunk and Supox come to mind)
	special_func: function () {
		if (this.special_counter) {
			--this.special_counter
		} else if (this.input_state.SPECIAL) {
			if (this.energy_level < this.info.special_energy_cost) {
				this.status.LOW_ON_ENERGY = true
				return
			}
			if (!this.race.init_special_func.apply(this)) return
			this.energy_level -= this.info.special_energy_cost
			this.special_counter = this.info.special_wait
		}
	},
	// Should return empty array or falsy if no weapons fired
	init_weapon_func: function () {
		return []
	},
	// Should return falsy if no special was triggered
	// Unlike init_weapon_func, this method is responsible for adding any elements to the world
	// that get spawned as a result of the special move.
	init_special_func: function () {
		return null
	}
}




